/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package fr.PERSOclient.utils;
/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

import fr.PERSOclient.Consts;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import fr.annuaireComp.entities.Personnel ;
/**
 *
 * @author MiniK
 */
public class AuthManager {
    
      public static final String  CURRENT_USER_SESSION_PARAM_NAME = "currentuser";
       
        public static final String  ERROR_MESSAGE_LOGIN_PAGE_PARAM_NAME = "errormsg";
         public static final String   LOGIN_REFERER_URI_PARAM ="ref";
        //   public static final String LOGIN_ERROR_TYPE_URI_PARAM = "errtype";
        public static Personnel  getCurrentUser(HttpServletRequest request)
        {
            return (Personnel )request.getSession().getAttribute(CURRENT_USER_SESSION_PARAM_NAME);
        
        }
        
      
          public static void setCurrentUser(HttpServletRequest request,Personnel  p )
        {
            request.getSession().setAttribute(CURRENT_USER_SESSION_PARAM_NAME, p);
        }
        
    /*      Retourne true si l'utilisateur est correctement loggué, false sinon     * 
           * 
           * 
           */
    public static boolean logUser(HttpServletRequest request, HttpServletResponse response)
    {
         Personnel currUser  = (Personnel )request.getSession().getAttribute(CURRENT_USER_SESSION_PARAM_NAME);
           if(currUser !=null)
             // if(Consts.IDFONCTION_AUTHORIZED.contains(currUser.getIdfonction().getIdfonction().toString()))
                return true;
          try {
              String qs ="";
              if(request.getQueryString() != null)
                  qs = "?" + request.getQueryString() ;
              String fullQuery = request.getRequestURI() + qs;
             response.sendRedirect("login?"+LOGIN_REFERER_URI_PARAM+"="+fullQuery );
        } catch (Exception e) {
        }
          return false;
        
    }
}
